#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define RI register int
#define m(x, y) make_pair(x, y)
const int N = 1e5 + 5;
int head[N], k, p[N], head1[N], k1, head2[N], k2;
bool vis[N];
int ind[N];
struct node
{
    int to;
    int val;
    int nxt;
}e[1000001], e1[1000001], e2[1000001];
void add2(int u, int v)
{
    e2[++k2].to = v;
    e2[k2].nxt = head2[u];
    head2[u] = k2;
}
void add1(int u, int v)
{
    e1[++k1].to = v;
    e1[k1].nxt = head1[u];
    head1[u] = k1;
}
void add(int u, int v)
{
    e[++k].to = v;
    e[k].nxt = head[u];
    head[u] = k;
}
int mi[N], mx[N], bel[N], dfn[N], ti, low[N], tot;
bool in[N], visb[N];
stack<int> S;
void tarjan(int now)
{
    low[now] = dfn[now] = ++ti;
    in[now] = true;
    S.push(now);
    for (int i = head[now]; i; i = e[i].nxt)
    {
        int to = e[i].to;
        if (!dfn[to])
            tarjan(to), low[now] = min(low[now], low[to]);
        else
            if (in[to])
                low[now] = min(dfn[to], low[now]);
    }
    if (dfn[now] == low[now])
    {
        int nt;
        tot++;
        do
        {
            nt = S.top();
            S.pop();
            in[nt] = false;
            bel[nt] = tot;
            mi[tot] = min(mi[tot], p[nt]);
            mx[tot] = max(mx[tot], p[nt]);
        } while (nt != now);
    }
}
inline int read()
{
    int p = 0, f = 1; char c = getchar();
    while (c < '0' || c>'9') { if (c == '-')f = -1; c = getchar(); }
    while (c >= '0' && c <= '9') { p = p * 10 + c - '0'; c = getchar(); }
    return f * p;
}
queue<int> Q;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    memset(mi, 101, sizeof mi);
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)cin >> p[i];
    for (int i = 1; i <= m; i++)
    {
        int x, y, z;
        cin >> x >> y >> z;
        add(x, y);
        if (z == 2)add(y, x);
    }
    for (int i = 1; i <= n; i++)
        if (!dfn[i])
            tarjan(1);
    for (int i = 1; i <= n; i++)
    {
        for (int j = head[i]; j; j = e[j].nxt)
        {
            int to = e[j].to;
            if (bel[i] != bel[to])
                add1(bel[i], bel[to]), ind[bel[to]]++, add2(bel[to], bel[i]);
        }
    }
    int ans = 0;
    Q.push(bel[1]);
    vis[bel[1]] = true;

    while (!Q.empty())
    {
        int now = Q.front();
        Q.pop();
        for (int i = head1[now]; i; i = e1[i].nxt)
        {
            int to = e1[i].to;
            if (!vis[to])
            {
                mi[to] = min(mi[now], mi[to]);
                ind[to]--;
                if (!ind[to])
                    Q.push(to), vis[to] = true;
            }
        }
    }

    Q.push(bel[n]);
    memset(vis, false, sizeof vis);
    vis[bel[n]] = true;
    while (!Q.empty())
    {
        int now = Q.front();
        Q.pop();
        ans = max(mx[now] - mi[now], ans);
        for (int i = head2[now]; i; i = e2[i].nxt)
        {
            int to = e2[i].to;
            if (!vis[to])
                Q.push(to), vis[to] = true;
        }
    }

    cout << ans;
    return 0;
}